home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
58606
/
58606.xpi
/
chrome
/
translator.jar
/
content
/
translatorUI.js
< prev
Wrap
Text File
|
2010-02-07
|
8KB
|
242 lines
(function(namespace, $)
{
namespace.TranslatorUI = function()
{
// empty constructor
};
namespace.TranslatorUI.prototype = {
init: function()
{
// set status panel listeners
$('#translator-status-icon, #translator-status-label').bind('click.translator', function(e) {
if(e.button === 0) {
$(document).trigger('translatorStateSwitched.translator');
}
}.bind(this));
// set toolbar button listeners
$('#translator-toolbarbutton')
.bind('command.translator', function(e) {
$(document).trigger('translatorStateSwitched.translator');
}.bind(this))
.bind('popupshowing.translator', function(e) {
if(e.target == $('#translator-toolbarbutton-menu').get(0)) {
$('#translator-context-menu').contents().appendTo(e.target);
}
}.bind(this))
.bind('popuphiding.translator', function(e) {
if(e.target == $('#translator-toolbarbutton-menu').get(0)) {
$(e.target).contents().appendTo('#translator-context-menu');
}
}.bind(this));
// set translator menu listeners
$('#translator-context-menuitem-enable, #translator-context-menuitem-disable').bind('command.translator', function(e) {
$(document).trigger('translatorStateSwitched.translator');
e.stopPropagation();
}.bind(this));
$('#translator-context-menuitem-translate').bind('command.translator', function(e) {
$(document).trigger('translatorTranslateSelection.translator');
e.stopPropagation();
}.bind(this));
$('#translator-context-languages-menu').bind('command.translator', this.languageChangedHandler.bind(this));
// set context menu popup listeners
$('#translator-content-area-menu')
.bind('popupshowing.translator', function(e) {
if(e.target == $('#translator-content-area-menupopup').get(0)) {
$('#translator-context-menuitem-translate').after(this.getMenuPlaceholder()).appendTo(e.target);
$('<menuseparator />').appendTo(e.target);
$('#translator-context-languages').after(this.getMenuPlaceholder()).appendTo(e.target);
}
}.bind(this))
.bind('popuphiding.translator', function(e) {
if(e.target == $('#translator-content-area-menupopup').get(0)) {
var placeholders = $('#translator-context-menu menuitem[placeholder="true"]');
$('#translator-context-menuitem-translate').insertAfter($(placeholders[0]));
$('#translator-context-languages').insertAfter($(placeholders[1]));
$(placeholders).remove();
$(e.target).empty();
}
}.bind(this));
// preferences listener
$('#translator-context-menuitem-preferences').bind('command.translator', function(e) {
this.openPreferences();
e.stopPropagation();
}.bind(this));
// show in context menu listener
$('#translator-context-menuitem-options-context').bind('command.translator', function(e) {
$(document).trigger('translatorShowContextMenuSwitched.translator', $(e.target).boolean('checked'));
e.stopPropagation();
}.bind(this));
// show in toolbar listener
$('#translator-context-menuitem-options-toolbar').bind('command.translator', function(e) {
$(document).trigger('translatorShowToolbarSwitched.translator', $(e.target).boolean('checked'));
e.stopPropagation();
}.bind(this));
// show in status bar listener
$('#translator-context-menuitem-options-statusbar').bind('command.translator', function(e) {
$(document).trigger('translatorShowStatusBarSwitched.translator', $(e.target).boolean('checked'));
e.stopPropagation();
}.bind(this));
},
showMessage: function(message)
{
},
showDefaultState: function()
{
$('#translator-status-bar, #translator-toolbarbutton').attr('state', 'enabled');
$('#translator-context-menuitem-enable').boolean('hidden', true);
$('#translator-context-menuitem-disable').boolean('hidden', false);
},
showErrorState: function()
{
$('#translator-status-bar, #translator-toolbarbutton').attr('state', 'error');
$('#translator-context-menuitem-enable').boolean('hidden', true);
$('#translator-context-menuitem-disable').boolean('hidden', false);
},
showDisabledState: function()
{
$('#translator-status-bar, #translator-toolbarbutton').attr('state', 'disabled');
$('#translator-context-menuitem-enable').boolean('hidden', false);
$('#translator-context-menuitem-disable').boolean('hidden', true);
},
showContextMenu: function(state)
{
$('#translator-content-area-menu').boolean('hidden', (!state));
$('#translator-context-menuitem-options-context').boolean('checked', state);
},
showToolbar: function(state)
{
var navBar = $('#nav-bar')[0];
var currentSet = navBar.currentSet;
// add toolbar button only if it is not there yet
if(currentSet.search(/translator-toolbarbutton-container/ig) == -1) {
currentSet = currentSet.replace(/urlbar-container/i, 'translator-toolbarbutton-container,urlbar-container');
$(navBar).attr('currentset', currentSet);
// overwrite current set
navBar.currentSet = currentSet;
// persist navbar
document.persist('nav-bar', 'currentset');
}
// toolbar should be shown after it was added to the panel
// it does NOT work otherwise
$('#translator-toolbarbutton').boolean('hidden', (!state));
$('#translator-context-menuitem-options-toolbar').boolean('checked', state);
},
showStatusBar: function(state)
{
$('#translator-status-bar').boolean('hidden', (!state));
$('#translator-context-menuitem-options-statusbar').boolean('checked', state);
},
showStatusBarIcon: function(state)
{
$('#translator-status-icon').boolean('hidden', (!state));
},
showStatusBarLabel: function(state)
{
$('#translator-status-label').boolean('hidden', (!state));
},
openPreferences: function()
{
window.openDialog('chrome://translator/content/preferences.xul', 'preferences', 'centerscreen,chrome');
},
enableHotKey: function()
{
// set keyboard shortcuts listeners
$('#translator-translate-key').unbind('command.translator').bind('command.translator', function(e) {
$(document).trigger('translatorTranslateSelection.translator');
e.stopPropagation();
});
},
disableHotKey: function()
{
$('#translator-translate-key').unbind('command.translator');
},
setHotKey: function(modifiersString, keyString)
{
$('#translator-translate-key')
.attr('modifiers', modifiersString)
.attr('key', keyString);
},
setLanguage: function(code, name)
{
// set status label text
$('#translator-status-label').val(name);
// set language in menu
$('#translator-context-languages-menu').children().each(function(i, menuitem) {
if(code.toLowerCase() == $(menuitem).attr('code').toLowerCase()) {
$(menuitem).boolean('checked', true);
}
}.bind(this));
},
loadLanguages: function(languages)
{
var $menu = $('#translator-context-languages-menu').eq(0).empty();
$.each(languages, function(code, name) {
var $menuitem = $(document.createElement('menuitem'));
$menuitem.attr('label', name);
$menuitem.attr('code', code);
$menuitem.attr('type', 'radio');
$menuitem.attr('name', 'language');
$menuitem.appendTo($menu);
}.bind(this));
},
getMenuPlaceholder: function()
{
return $('#translator-menuitem-placeholder').clone().removeAttr('id');
},
languageChangedHandler: function(e)
{
var languageCode = $(e.originalTarget).attr('code');
$(document).trigger('translatorLanguageChanged.translator', [languageCode]);
e.stopPropagation();
}
};
})(com.igorgladkov.translator, translatorJQuery);